#!/usr/bin/env python3.10
# -*- encoding: utf-8 -*-
'''
@File    :   paginator.py
@Time    :   2023/05/05 10:03:13
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib
import asyncio
from typing import Dict, List, Optional

from fastapi import Query
from tortoise.queryset import QuerySet

from .base import PageData


class PageSizePaginator:
    def __init__(self, max_size: int = 100):
        self.max_size = max_size
        self.page_num: int = 1
        self.page_size: int = 10
        self.order: List[str] = ['id']

    def __call__(self, page_num: int = Query(1, description='当前页码', alias='page'),
                 page_size: int = Query(10, description='每页数量', alias='pageSize'),
                 order: List[str] = Query(['id'], description='按指定字段排序,格式:-id 或 -create_time')):
        self.order = order
        self.page_num = max(page_num, 1)  # 如果传入的值小于1，按 1 算
        self.page_size = min(page_size, self.max_size)  # 如果超过 max_size ， 就算做是 maxsize
        return self

    async def output(self, queryset: QuerySet, filters: Optional[Dict] = None):
        if filters is None:
            filters = {}
        total, items = await asyncio.gather(
                queryset.filter(**filters).count(),
                queryset.limit(self.limit).offset(self.offset).order_by(*self.order).filter(**filters),
                )
        return PageData(items=items, total=total)

    @property
    def limit(self):
        return self.page_size

    @property
    def offset(self):
        return self.page_size * (self.page_num - 1)